using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CrazyDogToDo.Service.Services;
using CrazyDogToDo.Entity.Data;
using CrazyToDo.Web.Models;
using Microsoft.AspNetCore.Http;

namespace CrazyToDo.Web.Controllers
{
    public class ActivityController : Controller
    {
        private readonly ActivityService _activityService = new ActivityService();
        public IActionResult Index()
        {
            List<Activity> activitys = _activityService.GetAllActivity().ToList();
            List<ActivityViewModel> activityViewModels = activitys.Select(item => new ActivityViewModel
            {
                Id = item.Id,
                Theme = item.Theme,
                Content = item.Content,             
                PartNumber = item.PartNumber,               
            }).ToList();
            return View("Activity", activityViewModels);
        }
        /// <summary>
        /// 参加一个活动
        /// </summary>
        /// <param name="activityId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Join(int activityId)
        {
            _activityService.JoinActivity(HttpContext.Session.GetString("user"),activityId);
            return RedirectToAction("Index", "Home");
        }
        /// <summary>
        /// 添加一个活动
        /// </summary>
        /// <param name="theme"></param>
        /// <param name="content"></param>
        /// <param name="endTime"></param>
        /// <param name="partNumber"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(string theme, string content, string endTime, int partNumber) 
        {
            string email = HttpContext.Session.GetString("user");
            _activityService.CreateActivity(email,theme,content,endTime,partNumber);
            return RedirectToAction("Index");
        }
        
    }
}